<template>
  <div class="question-info">
    <div class="question-main">
      <div
        v-for="(item, index) of questionList"
        :key="index"
        class="question-item"
      >
        <div class="item-sort">{{ index + 1 }}</div>
        <div class="item-label">
          <div class="label-title">{{ item.entryTitle }}</div>
        </div>
        <div class="item-info">
          <div :class="['item-type', 'type-' + item.entryType]">
            {{ item.entryTypeName }}
          </div>
          <div class="item-content">
            <p v-if="item.entryType === '1' || item.entryType === '4'">
              {{ item.content }}
            </p>
            <template v-if="item.entryType === '2'">
              <van-radio
                v-for="(sItem, inx) of getContent(item)"
                :key="inx"
                disabled
                >{{ sItem.optionName }}</van-radio
              >
            </template>
            <template v-if="item.entryType === '3'">
              <div class="check-main">
                <div
                  class="check-item"
                  v-for="(sItem, inx) of getContent(item)"
                  :key="inx"
                >
                  <van-checkbox
                    shape="square"
                    v-model="sItem.checked"
                    disabled
                    >{{ sItem.optionName }}</van-checkbox
                  >
                </div>
              </div>
            </template>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts" name="QuestionInfo">
import { computed, ref } from "vue";
import { entryGetList } from "@/views/home/custom/comp/questionInfo/fetchData";
import { dictListAll } from "@/api/dict";

const props = defineProps({
  contentVoList: {
    type: Array,
    default: () => []
  }
});
const questionList = computed(() => {
  const arr = props.contentVoList || [];
  const contentVoList = arr.filter(item => item);
  contentVoList.forEach((item: any) => {
    entryList.value.forEach((sItem: any) => {
      if (item.entryId === sItem.id) {
        item.entryTitle = sItem.entryTitle;
      }
    });
  });
  entryList.value.forEach((sItem: any) => {
    sItem.entryTypeName = getType(sItem.entryType);
    contentVoList.forEach((item: any) => {
      if (item.entryId === sItem.id) {
        sItem.content = item.content;
      }
    });
  });
  return entryList.value;
});
const entry_type = ref<Record<string, any>[]>([]);
const entryList = ref<Record<string, any>[]>([]);

function getType(type: string) {
  const part = entry_type.value.find((item: any) => item.dictValue === type);
  return part?.dictLabel;
}

function getContent(item: any) {
  const part = entryList.value.find((sItem: any) => item.id === sItem.id);
  const optionVoList = part?.optionVoList || [];
  const ids = item.content?.split(",") || [];
  const arr = optionVoList.filter((sItem: any) => ids.includes(sItem.id));
  if (item.entryType === "3")
    arr.forEach((sItem: any) => (sItem.checked = true));
  return arr;
}

entryGetList().then(res => {
  entryList.value = res.data || [];
});

dictListAll("entry_type").then(res => {
  entry_type.value = res.data || [];
});
</script>

<style scoped lang="less">
.question-info {
  position: relative;
  width: 100%;

  .question-title {
    margin-bottom: 20px;
    font-weight: 700;
    font-size: 20px;
    color: #2e3244;
  }

  .question-main {
    width: 100%;
    background: #f9f9f9;

    .question-item {
      position: relative;
      margin: 0 1.2rem 0 2.5rem;
      padding: 0.4rem 0;
      display: flex;
      flex-direction: column;
      justify-content: center;
      border-bottom: 1px solid rgba(187, 187, 187, 0.5);

      .item-sort {
        position: absolute;
        top: 50%;
        left: -2.2rem;
        transform: translateY(-50%);
        width: 5rem;
        height: 2rem;
        padding-left: 0.6rem;
      }

      &:first-child .item-sort {
        color: #ff0012;
        background: linear-gradient(
          90deg,
          rgba(255, 30, 28, 0.3) 0%,
          rgba(255, 49, 99, 0) 100%
        );
      }

      &:nth-child(2) .item-sort {
        color: #ff9100;
        background: linear-gradient(
          90deg,
          #ffa900 0%,
          rgba(255, 211, 37, 0) 100%
        );
      }

      &:nth-child(3) .item-sort {
        color: #9e5600;
        background: linear-gradient(
          96deg,
          #ff9a42 0%,
          rgba(238, 111, 0, 0) 100%
        );
      }

      .item-label {
        margin-bottom: 0.4rem;
        font-weight: 700;
        color: #2e3244;

        .label-title {
          margin-right: 1.2rem;
        }
      }

      .item-info {
        display: flex;
        align-items: center;
        color: #999;

        .item-content {
          width: auto;
        }

        .item-type {
          width: 3.2rem;
          height: 1.2rem;
          margin-right: 0.8rem;
          font-size: 0.9rem;
          line-height: 1.2rem;
          text-align: center;
          color: #fff;

          &.type-1 {
            background: #4fd278;
          }

          &.type-2 {
            background: #8d7fe0;
          }

          &.type-3 {
            background: #fbb643;
          }

          &.type-4 {
            background: #54a6f7;
          }
        }

        .check-main {
          display: flex;
          flex-wrap: wrap;

          .check-item {
            margin-right: 0.8rem;
            margin-bottom: 0.4rem;
          }
        }
      }
    }
  }
}
</style>
